package member.util;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import org.apache.tomcat.dbcp.dbcp.BasicDataSource;

/*
 * 1. DataSource에서부터 Connection을 가져와 return해주는 역할
 * 		- getConnection() : java.sql.Connection
 * 2. JDBC 관련 객체들을 close() 하는 역할
 * 		- close(conn, pstmt, rset)
 *     - close(conn, pstmt)
 * ** 싱글톤 패턴으로 작성.     
 */
public class DatabaseUtil {
	private static DatabaseUtil instance = new DatabaseUtil();
	private BasicDataSource ds;
	private DatabaseUtil(){
		ds = new BasicDataSource();
//		DB연결 설정
		ds.setDriverClassName("oracle.jdbc.driver.OracleDriver");
		ds.setUrl("jdbc:oracle:thin:@127.0.0.1:1521:XE");
		ds.setUsername("scott");
		ds.setPassword("tiger");
	}
	public static DatabaseUtil getInstance(){
		return instance;
	}
	//Connection 리턴하는 메소드
	public Connection getConnection() throws SQLException{
		return ds.getConnection();
	}
	
	//연결 close 메소드
	public void close(PreparedStatement pstmt, Connection conn) throws SQLException{
		if(pstmt!=null) pstmt.close();
		if(conn!=null) conn.close();
	}
	public void close(ResultSet rset, PreparedStatement pstmt, Connection conn)
								throws SQLException{
		if(rset!=null) rset.close();
		if(pstmt!=null) pstmt.close();
		if(conn!=null) conn.close();
	}
}











